package com.black.common.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.black.common.mybatis.datascope.QueryDataScopeHandler;
import com.black.common.mybatis.datascope.DataScopeHandlerInterceptorRegistry;
import com.black.common.mybatis.service.DataScopeService;
import com.black.common.mybatis.service.impl.DataScopeServiceImpl;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author linwenfeng
 */
@Configuration
@ConditionalOnClass({MybatisPlusAutoConfiguration.class})
@AutoConfigureAfter(MybatisPlusAutoConfiguration.class)
@EnableTransactionManagement
public class MybatisPlusConfig {

    @Bean
    public DataScopeService dataScopeService() {
        return new DataScopeServiceImpl();
    }

    @Bean
    public DataScopeInterceptor dataScopeInterceptor() {
        return new DataScopeInterceptor();
    }

    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new BaseEntityMetaObjectHandler();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(dataScopeInterceptor());
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public QueryDataScopeHandler queryDataScopeHandler() {
        return new QueryDataScopeHandler();
    }

    @Bean
    public DataScopeHandlerInterceptorRegistry dataScopeHandlerInterceptorRegistry() {
        return new DataScopeHandlerInterceptorRegistry();
    }

}
